package com.kma.summer2012.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: EUGENE
 * Date: 3/7/13
 * Time: 12:15 AM
 * To change this template use File | Settings | File Templates.
 */
public class DialogBoxFactory {
    private static DialogBoxFactory myself;            // TODO why singlton ?

    public static DialogBox getDialogBox(Widget content, String name) {
        if (myself == null) {
            myself = new DialogBoxFactory();
        }
        List<HasClickHandlers> list = new ArrayList<HasClickHandlers>();
        list.add(new Button("Close"));
        return myself.createDialogBox(content, name, list);
    }

    public static DialogBox getDialogBoxUncloseable(Widget content, String name) {
        if (myself == null) {
            myself = new DialogBoxFactory();
        }
        return myself.createDialogBox(content, name, null);
    }
    public static DialogBox createUnclousableMessageBox(String message, String name){
        if (myself == null) {
            myself = new DialogBoxFactory();
        }
        HTML text = new HTML(message);
        return myself.createDialogBox(text, name, null);

    }
    public static DialogBox createMessageBox(String message, String name) {
        if (myself == null) {
            myself = new DialogBoxFactory();
        }
        HTML text = new HTML(message);
        List<HasClickHandlers> list = new ArrayList<HasClickHandlers>();
        list.add(new Button("Close"));
        return myself.createDialogBox(text, name, list);
    }

    public static DialogBox createMessageBox(String message, String name, List<HasClickHandlers> list) {
        if (myself == null) {
            myself = new DialogBoxFactory();
        }
        Widget a = new Widget();
        if (a instanceof HasClickHandlers) {
        }
        HTML text = new HTML(message);
        list.add(new Button("Close"));
        return myself.createDialogBox(text, name, list);
    }


    private DialogBox createDialogBox(Widget content, String name, List<HasClickHandlers> buttons) {
        // Create a dialog box and set the caption text
        final DialogBox dialogBox = new DialogBox();
        dialogBox.ensureDebugId("cwDialogBox");
        dialogBox.setText(name);

        // Create a table to layout the content
        VerticalPanel dialogContents = new VerticalPanel();
        dialogContents.setSpacing(4);
        dialogBox.setWidget(dialogContents);


        HTML details = new HTML("DETAILS");
        dialogContents.add(content);
        dialogContents.setCellHorizontalAlignment(
                details, HasHorizontalAlignment.ALIGN_CENTER);

        HorizontalPanel buttonPanel = new HorizontalPanel();
        buttonPanel.setSpacing(5);
        dialogContents.add(buttonPanel);
        if (LocaleInfo.getCurrentLocale().isRTL()) {
            dialogContents.setCellHorizontalAlignment(
                    buttonPanel, HasHorizontalAlignment.ALIGN_LEFT);

        } else {
            dialogContents.setCellHorizontalAlignment(
                    buttonPanel, HasHorizontalAlignment.ALIGN_RIGHT);
        }
        //Button closeButton = new Button(
        //      "CLOSE ", new ClickHandler() {
        ///public void onClick(ClickEvent event) {
        // dialogBox.hide();
        //}
        //});
        if (buttons != null) {
            for (HasClickHandlers button : buttons) {
                button.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent clickEvent) {
                        dialogBox.hide();
                    }
                });
                buttonPanel.add((Widget) button);
            }
        }

        dialogBox.setGlassEnabled(true);
        dialogBox.setAnimationEnabled(true);
        dialogBox.center();
        dialogBox.getElement().getStyle().setZIndex(1000000);
        return dialogBox;
    }
}
